package server;

import client.clientWindow;

/**
 *
 * @author buyandalai
 */
public class window extends javax.swing.JFrame {
    /** Creates new form window */
    private server srvr = null;
    public window() {
        initComponents();
        stop.setEnabled(false);
    }


    public void startServer()
    {
        if(port.getText().equals(""))
            javax.swing.JOptionPane.showMessageDialog(null, "Портын дугааараа оруулна уу ",
                                         "Алдаа!",
                                         javax.swing.JOptionPane.ERROR_MESSAGE);

        port.setEnabled(false);
        srvr = new server(this);
        srvr.start();
        start.setEnabled(false);
        stop.setEnabled(true);
        this.message("Server аслаа.");
        status.setText("Server ажиллаж байна...");
    }

    public void stopServer()
    {
        port.setEnabled(true);
        srvr.close();
        srvr.stop();
        start.setEnabled(true);
        status.setText("Server зогссон!");
        this.message("Server stopped.");
    }

    @SuppressWarnings("unchecked")
    
    private void initComponents() {

        start = new javax.swing.JButton();
        info_sc = new javax.swing.JScrollPane();
        info = new javax.swing.JTextArea();
        status = new javax.swing.JLabel();
        port = new javax.swing.JTextField();
        labe_port = new javax.swing.JLabel();
        stop = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        connect = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        start.setText("Start");
        start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startActionPerformed(evt);
            }
        });

        info.setColumns(20);
        info.setEditable(false);
        info.setRows(5);
        info_sc.setViewportView(info);

        status.setText("Server is stopped!");

        port.setText("6666");

        labe_port.setText("Port:");

        stop.setText("Stop");
        stop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Trebuchet MS", 3, 24));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("LAN Chatting System");
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        connect.setText("Connect to another server");
        connect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)
                    .addComponent(info_sc, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(status)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 127, Short.MAX_VALUE)
                        .addComponent(labe_port)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(start)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(stop))
                    .addComponent(connect, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(status)
                    .addComponent(start)
                    .addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labe_port)
                    .addComponent(stop))
                .addGap(18, 18, 18)
                .addComponent(info_sc, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(connect)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void startActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        startServer();
    }

    private void stopActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        stopServer();
    }

    private void connectActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        clientWindow cw = new clientWindow();
        cw.setVisible(true);
}


    public int getPort()
    {
        return Integer.parseInt(port.getText());
    }

    public void client_connected(String ip)
    {
        info.setEnabled(true);
        this.message("Client connected from "+ip);
    }

    public void message(String str)
    {
        info.setText(str+"\n"+info.getText());
    }


    // Variables declaration - do not modify
    private javax.swing.JButton connect;
    private javax.swing.JTextArea info;
    private javax.swing.JScrollPane info_sc;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel labe_port;
    private javax.swing.JTextField port;
    private javax.swing.JButton start;
    private javax.swing.JLabel status;
    private javax.swing.JButton stop;
    // End of variables declaration

}
